<?php
namespace app\admin\controller;
 use think\Controller;
use think\Db;
use think\Request;


class Model extends Common
{


    public function index()
    {
        $item='';
        $list = Db::name('model')->order("disorder desc,id desc")->paginate()->each(function($item='', $key=''){
            $item['model_fields_str'] = $this->get_model_name($item['model_fields']);
            return $item;
        });
        // print_r(config('model_fields'));die;
        $this->assign("list",$list);
        return $this->fetch("index");
    }

    public function add()
    {
        $id=input('id',0);
        $model_fields=config('model_fields');
        $db=Db::name("model");
 		$v=$db->find($id);
 		if($v){
            // print_r($v);
        }else{
            $v['status']=1;
            $v['model_fields']="";
            $v['model_name']="";
            $v['model_c']="";
            $v['model_a']="";
            $v['id']="";
            $v['disorder']=$db->max('disorder')+5;
        }
        $arr=$this->get_frm_out_put($model_fields,"model_fields","checkbox",$v['model_fields'],"","");
        if(Request::instance()->isPost()){
            $data=$_POST;
            $data['model_fields']=implode($_POST['model_fields'],",");
            if($id){
                if(Db::name("model")->where("id=".$id)->update($data)){
                    $this->success("修改成功","/admin/model/index.html");
                }else{
                    $this->error("修改失败");
                }
            }else{
                if(Db::name("model")->insert($data)){
                    $this->success("添加成功","/admin/model/index.html");
                }else{
                    $this->error("添加失败");
                }
            }

        }

        $this->assign("model_fields",$arr);
        $this->assign("v",$v);
        return $this->fetch("add");
    }


    public function del()
    {
        if(Request::instance()->isGet()){
            $get=Request::instance()->param();
            $id=$get['id'];
            if(Db::name('model')->delete($id)){
                $this->success("删除成功","/admin/model/index.html");
            }else{
                $this->error("删除失败");
            }
        }
    }



    public function audit()
    {
        $id=input('id',0);

        $v=Db::name("model")->find($id);
        $data['status']=$v['status']?0:1;

        if(Db::name("model")->where("id=".$id)->update($data)){
            $this->success("审核成功","/admin/model/index.html");
        }else{
            $this->error("审核失败");
        }


    }
}